#!/usr/bin/env ruby



indivs = {
  'http://www.aifb.kit.edu/agents#NGO' => ['http://greenpeace.org/#gp',
                                           'http://bund.de/#wir',
                                           'http://oekos.com/#oeko',
                                           'http://ecologists.org/#us',
                                           'http://charity.com/#ch'],
  'http://www.aifb.kit.edu/agents#ApplianceManufacturer' =>
  ['http://coolwash.com/#company',
   'http://hotwash.com/#hot',
   'http://gadgetfactory.com/#us',
   'http://appliances.de/#wir',
   'http://fridgeman.com/#comp'],
  'http://www.aifb.kit.edu/agents#EnergyProducer' =>
  ['http://powercomp.ex/Company',
   'http://energy.producer/#us',
   'http://energielieferant.de/#wir',
   'http://maxpowers.com/#comp',
   'http://nuclearplants.net/#np'],
  'http://www.aifb.kit.edu/agents#LawEnforcement' =>
  ['http://government.us/#gov',
   'http://fbi.com/#fbi',
   'http://bka.de/#bka',
   'http://dieregierungs.de/#r',
   'http://geheimdienst.org/#gd']
}

purposes = ['http://www.aifb.kit.edu/purposes#WelfareConsulting',
            'http://www.aifb.kit.edu/purposes#Statistics',
            'http://www.aifb.kit.edu/purposes#CommercialConsulting',
            'http://www.aifb.kit.edu/purposes#Billing',
            'http://www.aifb.kit.edu/purposes#LawEnforcement']
prototypes = [
              {:rec => 'http://www.aifb.kit.edu/agents#NGO',
                :purp => 'http://www.aifb.kit.edu/purposes#WelfareConsulting'},
              {:rec => 'http://www.aifb.kit.edu/agents#NGO',
                :purp => 'http://www.aifb.kit.edu/purposes#Statistics'},
              {:rec => 'http://www.aifb.kit.edu/agents#ApplianceManufacturer',
                :purp => 'http://www.aifb.kit.edu/purposes#CommercialConsulting'},
              {:rec => 'http://www.aifb.kit.edu/agents#EnergyProducer',
                :purp => 'http://www.aifb.kit.edu/purposes#Statistics'},
              {:rec => 'http://www.aifb.kit.edu/agents#EnergyProducer',
                :purp => 'http://www.aifb.kit.edu/purposes#Billing'},
              {:rec => 'http://www.aifb.kit.edu/agents#LawEnforcement',
                :purp => 'http://www.aifb.kit.edu/purposes#LawEnforcement'}
]

perspectives = ['http://www.aifb.kit.edu/lieschenpolicy#Anonymized',
                'http://www.aifb.kit.edu/lieschenpolicy#CoolWashPersp',
                'http://www.aifb.kit.edu/lieschenpolicy#HourlyUsage',
                'http://www.aifb.kit.edu/lieschenpolicy#DailyUsage',
                'http://www.aifb.kit.edu/lieschenpolicy#MonthlyUsage']


# Create Requests
reqFile = "request0"
# (a) 1 for each prototype with indiv randomly seleced from agent class
prototypes.each do |prot|
  open("#{reqFile}.n3","w") do |out|
    out.puts "@prefix p: <http://www.aifb.kit.edu/policy#> ."
    out.puts "<http://request.com/\##{reqFile}> a p:Request ;"
    out.puts "  p:purpose <#{prot[:purp]}> ;"
    out.puts "  p:recipient <#{indivs[prot[:rec]][rand 5]}> ."
  end
  reqFile = reqFile.next
end

# (b) 4 randomly

4.times do 
open("#{reqFile}.n3","w") do |out|
    out.puts "@prefix p: <http://www.aifb.kit.edu/policy#> ."
    out.puts "<http://request.com/\##{reqFile}> a p:Request ;"
    out.puts "  p:purpose <#{purposes[rand(purposes.length)]}> ;"
    out.puts "  p:recipient <#{indivs[indivs.keys[rand(indivs.length)]][rand 5]}> ."
  end
  reqFile = reqFile.next
end

# Create Allowed Usages
alloweds = []

allowedName = "allowedUsage00"
perspectives.each do |perspective|
  # Create 6 Prototypes and for each 2 Indivis
  prototypes.each do |prot|
    str = "\n" + # "@prefix p: <http://www.aifb.kit.edu/policy#> .\n" +
      "<http://www.aifb.kit.edu/lieschenpolicy\##{allowedName}> a p:AllowedUsage ;\n" +
      "  p:perspective <#{perspective}> ;\n" +
      "  p:purpose <#{prot[:purp]}> ;\n"  +
      "  p:recipient <#{prot[:rec]}> .\n"
    alloweds << {:uri => "http://www.aifb.kit.edu/lieschenpolicy\##{allowedName}",
      :def => str}
    allowedName = allowedName.next
    str = "\n" + # "@prefix p: <http://www.aifb.kit.edu/policy#> .\n" +
      "<http://www.aifb.kit.edu/lieschenpolicy\##{allowedName}> a p:AllowedUsage ;\n" +
      "  p:perspective <#{perspective}> ;\n" +
      "  p:purpose <#{prot[:purp]}> ;\n"  +
      "  p:recipient <#{indivs[prot[:rec]][rand 5]}> .\n"
    alloweds << {:uri => "http://www.aifb.kit.edu/lieschenpolicy\##{allowedName}",
      :def => str}
    allowedName = allowedName.next
    str = "\n" + # "@prefix p: <http://www.aifb.kit.edu/policy#> .\n" +
      "<http://www.aifb.kit.edu/lieschenpolicy\##{allowedName}> a p:AllowedUsage ;\n" +
      "  p:perspective <#{perspective}> ;\n" +
      "  p:purpose <#{prot[:purp]}> ;\n"  +
      "  p:recipient <#{indivs[prot[:rec]][rand 5]}> .\n"
    alloweds << {:uri => "http://www.aifb.kit.edu/lieschenpolicy\##{allowedName}",
      :def => str}
    allowedName = allowedName.next
  end
  
  # Create 2 Randoms
  2.times do
    str = "\n" + # "@prefix p: <http://www.aifb.kit.edu/policy#> .\n" +
      "<http://www.aifb.kit.edu/lieschenpolicy\##{allowedName}> a p:AllowedUsage ;\n" +
      "  p:perspective <#{perspective}> ;\n" +
      "  p:purpose <#{purposes[rand(purposes.length)]}> ;\n"  +
      "  p:recipient <#{indivs[indivs.keys[rand(indivs.length)]][rand 5]}> .\n"
    alloweds << {:uri => "http://www.aifb.kit.edu/lieschenpolicy\##{allowedName}",
      :def => str}
    allowedName = allowedName.next
  end
end

# Cycle through sizes: 1,5,10, ... , 100

policyName = "policySize"

([1] + (1..20).map { |i| i * 5 }).each do |size|
  # Create 10 Policies
  nr = 0
  10.times do
    # Each policy has {size} random allowed usages
    policyName = "policySize#{size}Nr#{nr}"
    # Get the {size} random usages
    usages = (0..99).to_a.sort{ rand() - 0.5 }[0..size-1]

    open("#{policyName}.n3","w") do |out|
      out.puts "@prefix p: <http://www.aifb.kit.edu/policy#> ."
      out.puts "<http://request.com/\##{policyName}> a p:PrivatePolicy ;"
      usages.each do |u|
        out.puts "  p:allows <#{alloweds[u][:uri]}> ;"
      end
      out.puts '  p:validFrom "2010-01-01T00:00:00" ;'
      out.puts '  p:validTo "2010-12-31T23:59:59" .'
      out.puts ''
      usages.each do |u|
        out.puts alloweds[u][:def]
      end
    end
    nr += 1
  end
end


